<template>
	<view class="login" :style="{height:wh+'px'}">
		<!-- 显示上方的图片 -->
		<view class="title">
			<image src="../../static/images/register.png" mode="aspectFit"></image>
			<image src="../../static/images/register2.png" mode="aspectFit"></image>
		</view>
		<!-- 下方的表单 -->
		<view class="content">
			<uni-forms ref="from" :model="regForm" :rules="rules" validateTrigger="blur">
				<uni-forms-item name="username">
					<uni-easyinput v-model="regForm.username" placeholder="请输入用户名" />
				</uni-forms-item>
				<uni-forms-item name="password">
					<uni-easyinput type="password" v-model="regForm.password" placeholder="请输入密码" />
				</uni-forms-item>
				<uni-forms-item name="confirmPassword">
					<uni-easyinput type="password" v-model="regForm.confirmPassword" placeholder="请确认密码" />
				</uni-forms-item>
			</uni-forms>
			<button class="button" type="primary" @click="toRegister(from)">注册</button>
		</view>
		<view class="register">
			<navigator url="/pages/login/login">还没有账号 ，点击登录</navigator>
		</view>
	</view>

</template>

<script setup>
	import {
		ref,
		computed
	} from 'vue';

	import {
		userStore
	} from "../../store/userStore";

	import {
		CONFIG
	} from '../../utils/config';

	let store = userStore();
	let wh = computed(() => {
		let h = uni.getWindowInfo().windowHeight;
		console.log("h=" + h);
		return h;
	});

	const from = ref(null)


	const regForm = ref({
		username: '',
		password: '',
	});

	function toRegister(from) {
		from.validate().then(res => {
			//验证通过,进行接口请求用户注册
			uni.$post(`${CONFIG.leanbase}/1.1/classes/user?`, regForm.value).then(res2 => {
				//返回码400表示注册失败。显示错误信息
				if (res2.code == 400) {
					uni.showToast({
						title: res2.message,
						icon: "error"
					})
					return;
				}
				// 返回吗200表示注册成功，显示显示成功信息，并返回
				if (res2.code == 200) {
					uni.showToast({
						title: "登录成功",
						icon: "success"
					})
					// uni.navigateTo({
					// 	url:"/pages/Login/Login"
					// })
				}
				uni.navigateBack()
			})
		}).catch(err => {
			// 验证失败
			console.log(err);
		})
	}


	// 验证规则
	const rules = ref({
		username: {
			rules: [{
				required: true,
				errorMessage: '请输入用户名'
			}, {
				minLength: 3,
				maxLength: 10,
				errorMessage: '用户必须在{minLength}到{maxLength}个字符之间'
			}, {
				pattern: '^\\w{3,10}$',
				errorMessage: '请输入3-10位数字字母和下划线作为用户名'
			}]
		},
		password: {
			rules: [{
				required: true,
				errorMessage: '请输入密码'
			}, {
				pattern: '^[a-zA-Z0-9._!@#$%^&]{3,10}$',
				errorMessage: '请输入3-10位数字字母和特殊字符密码'
			}]
		},
		confirmPassword: {
			rules: [{
					required: true,
					errorMessage: "请输入密码"
				},
				{
					pattern: '^[a-zA-Z0-9._!@#$%^&]{3,10}$',
					errorMessage: '请输入3-10位数字字母和特殊字符密码'
				},
				{
					validateFunction: (rule, value, data, callback) => {
						console.log(value, data.password);
						if (value != data.password) {
							// 验证失败,执行callback("......")
							callback("两次输入的密码不一致")
						}
						//验证成功，返回true
						return true
					}
				}
			]
		}
	});
</script>

<style scoped>
	.login {
		border: 1px solid red;
		position: relative;
	}

	.login .title {
		margin-top: 100px;
	}

	.login .title image:nth-child(1) {
		height: 50px;
	}

	.login .title image:nth-child(2) {
		height: 30px;
	}

	.login .content {
		margin: 10px 20px;
	}

	.login .register {
		position: absolute;
		bottom: 20px;
		width: 100vw;
		text-align: center;
		color: #007aff;
		font-size: 20rpx;
	}
</style>